﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;

namespace 第二题
{

    [Serializable]
    public class student
    {
        public string num
        { get; set; }
        public string name
        { get; set; }
        public int age
        { get; set; }
        public student() { }
        public student(string mynum, string myname, int myage)
        {
            num = mynum;
            name = myname;
            age = myage;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            List<student> l = new List<student>();
            student s1 = new student("000", "tom", 21);
            student s2 = new student("001", "jack", 19);
            student s3 = new student("002", "alice", 20);
            l.Add(s1);
            l.Add(s2);
            l.Add(s3);

            //保存在json格式的文件中
            FileStream fs = new FileStream(@"D:\Students.txt", FileMode.Create);
            var options = new JsonSerializerOptions();
            options.WriteIndented = true;
            Utf8JsonWriter jw = new Utf8JsonWriter(fs);
            JsonSerializer.Serialize(jw, l, options);
            fs.Close();

            //按学号查询学生
            Console.WriteLine("请输入要查询的学生学号：");
            string s = Convert.ToString(Console.ReadLine());
            foreach (student x in l)
            {
                if (x.num == s)
                {
                    Console.WriteLine("查询成功！");
                    Console.WriteLine("学号：" + x.num);
                    Console.WriteLine("姓名：" + x.name);
                    Console.WriteLine("年龄：" + x.age);
                    break;
                }
            }

            //增加学生
            student s4 = new student();
            Console.WriteLine("请输入要添加的学生的学号：");
            string a = Convert.ToString(Console.ReadLine());
            Console.WriteLine("请输入要添加的学生的姓名：");
            string b = Convert.ToString(Console.ReadLine());
            Console.WriteLine("请输入要添加的学生的年龄：");
            int c = Convert.ToInt32(Console.ReadLine());
            s4.num = a;
            s4.name = b;
            s4.age = c;
            l.Add(s4);
            Console.WriteLine("增加成功！");

            //修改学生属性值
            Console.WriteLine("请输入要修改的学生的学号：");
            string ss = Convert.ToString(Console.ReadLine());
            Console.WriteLine("请输入要修改后的学生的年龄：");
            int d = Convert.ToInt32(Console.ReadLine());
            foreach (student x in l)
            {
                if (x.num == ss)
                {
                    x.age = d;
                    Console.WriteLine("修改成功！");
                    Console.WriteLine("修改后：");
                    Console.WriteLine("学号：" + x.num);
                    Console.WriteLine("姓名：" + x.name);
                    Console.WriteLine("年龄：" + x.age);
                    break;
                }
            }

            //删除指定学号的学生
            Console.WriteLine("请输入要删除的学生的学号：");
            string sss = Convert.ToString(Console.ReadLine());
            foreach (student x in l)
            {
                if (x.num == sss)
                {
                    l.Remove(x);
                    Console.WriteLine("删除成功！");
                    break;
                }
            }

            //总体
            Console.WriteLine("修改后的学生信息：");
            foreach (student x in l)
            {
                Console.WriteLine(x.num);
                Console.WriteLine(x.name);
                Console.WriteLine(x.age);
            }

            //排序
            Console.WriteLine("排序后的学生信息：");
            foreach (student x in l)
            {
                Console.WriteLine(x.num);
                Console.WriteLine(x.name);
                Console.WriteLine(x.age);
            }
            Console.ReadKey();
        }

    }
}